package com.hspedu.homework.homework02;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/3/3
 * @time 18:13
 */
public class Homework02ReceiverA {
    public static void main(String[] args) throws IOException {

        DatagramSocket datagramSocket = new DatagramSocket(8888);
        byte[] buf = new byte[1024];
        DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length);
        datagramSocket.receive(datagramPacket);

        byte[] data = datagramPacket.getData();
        String strData = new String(data, datagramPacket.getOffset(), datagramPacket.getLength());
        System.out.println(strData);

        if ("四大名著是那些？".equals(strData)){
            byte[] book = "红楼梦".getBytes();
            DatagramPacket datagramPacket2 = new DatagramPacket(book,0,book.length, InetAddress.getLocalHost(),9998);
            datagramSocket.send(datagramPacket2);
        }else {
            byte[] what = "what?".getBytes();
            DatagramPacket datagramPacket2 = new DatagramPacket(what,0,what.length,InetAddress.getLocalHost(),9998);
            datagramSocket.send(datagramPacket2);
        }

        datagramSocket.close();
    }
}
